package com.tfswue.springboot_demo02.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.springboot_demo02.entity.User; // 正确导入
import com.tfswue.springboot_demo02.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/users")
@Tag(name = "用户接口",description = "符合Restful风格的用户接口")
public class RestFulUserController { // 类名和文件名一致
    @Autowired
    private UserService userService;

    // 查询所有用户
    @Operation(summary = "查询所有用户",description = "查询所有用户")
    @GetMapping
    @ApiResponse(responseCode = "200", description = "查询所有用户返回用户list")
    public List<User> getUsers() {
        return userService.list();
    }

    // 查询单个用户，根据id
    @Cacheable(value = "dataCache", key = "#id")
    @GetMapping("/{id}/{name}")
    public User getUserById(@Parameter(description = "用户的id", required = true) @PathVariable("id") Integer id, @PathVariable("name") String name) {
        log.info("id:{}", id, name);
        return userService.getById(id);
    }
    //分页查询
    @GetMapping("/querypage/{page}/{size}")
    public List<User> queryPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<User> testpage = new Page<>(page, size);
        Page<User> userPage = userService.page(testpage, null);
        return userPage.getRecords();
    }
    //进行多条件分页查询
    @PostMapping("/querylist/{page}/{size}")
    public List<User> querylist(@PathVariable("page") Integer page,
                                @PathVariable("size") Integer size,
                                @RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user.getUsername() != null){
            queryWrapper.like("username", user.getUsername());
        }
        if (user.getEmail() != null){
            queryWrapper.like("email", user.getEmail());
        }
        Page<User> testpage = new Page<>(page, size);
        Page<User> userPage = userService.page(testpage, queryWrapper);
        return userPage.getRecords();
    }
    //添加用户
    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);
    }
    //修改用户
    @CachePut(value = "dataCache", key = "#id")
    @PutMapping
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }
    //删除用户
    @CacheEvict(value = "dataCache", key = "#id")
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Integer id) {
        return userService.removeById(id);
    }
}